#include "widget.h"
#include "ui_widget.h"
#include "cqtgpsSingle.h"
#include "cdcelecenermeter.h"
#include "cacelecenermeter.h"
#include "cdaccontroler.h"
#include "cdaccontrolersingle.h"
#include "cqtgps.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //dac 发送数据
//    CDacControlerSingle::GetSingelton()->write("123");
//    CDacControlerSingle::GetSingelton()->write("123");
//    CDacControlerSingle::GetSingelton()->write("123");
//    CDacControlerSingle::GetSingelton()->write("123");
//    CDacControlerSingle::GetSingelton()->set1RoadDacOutput(2.3);
//    CDacControlerSingle::GetSingelton()->setBatterySimulatorOutput(300);


//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
//    CQtGpsSingle::GetSingelton()->write("123");
#if 1 //测试饿汉单例模式
   // if( CQtGpsSingle::GetSingelton()->isOpen()==true)
    {
//        qDebug()<<tr("open true");
//        CQtGpsSingle::GetSingelton()->write("hello");
            CDacControlerSingle::GetSingelton()->write("hello");
        connect(CQtGpsSingle::GetSingelton(),&CQtGpsSingle::signalGetReady,this,[=]
        {
            CDacControlerSingle::GetSingelton()->write("get signal");
            this->ui->lineEditTime->setText(CQtGpsSingle::GetSingelton()->getCurrentTime().toString("yyyy-MM-dd hh:mm:ss"));
            //this->ui->lineEditTime->setText("QYROIUYEW");
//            qDebug()<<"当前时刻："<<CQtGpsSingle::GetSingelton()->getCurrentTime()<<"纬度："<<CQtGpsSingle::GetSingelton()->getLatitudeSign()
//                   <<CQtGpsSingle::GetSingelton()->getLatitude()<<"纬度"<<CQtGpsSingle::GetSingelton()->getLongitudeSign()<<CQtGpsSingle::GetSingelton()->getLongitude();
        });
    }

    connect(CQtGpsSingle::GetSingelton(),&CQtGpsSingle::readyRead,this,[=]
    {
        CDacControlerSingle::GetSingelton()->write("get signal1");
        this->ui->lineEditTime->setText(CQtGpsSingle::GetSingelton()->getCurrentTime().toString("yyyy-MM-dd hh:mm:ss"));
        //this->ui->lineEditTime->setText("QYROIUYEW");
//            qDebug()<<"当前时刻："<<CQtGpsSingle::GetSingelton()->getCurrentTime()<<"纬度："<<CQtGpsSingle::GetSingelton()->getLatitudeSign()
//                   <<CQtGpsSingle::GetSingelton()->getLatitude()<<"纬度"<<CQtGpsSingle::GetSingelton()->getLongitudeSign()<<CQtGpsSingle::GetSingelton()->getLongitude();
    });

    //测试ttyAMA2 信号槽成功
    connect(CDacControlerSingle::GetSingelton(),&CDacControlerSingle::readyRead,this,[=]
    {
        CDacControlerSingle::GetSingelton()->write("get signal DAC\r\n");
         CDacControlerSingle::GetSingelton()->write(CDacControlerSingle::GetSingelton()->readAll().toStdString().c_str());
          CDacControlerSingle::GetSingelton()->write("\r\n");
        //this->ui->lineEditTime->setText(CQtGpsSingle::GetSingelton()->getCurrentTime().toString("yyyy-MM-dd hh:mm:ss"));
        this->ui->lineEditTime->setText("DAC");
//            qDebug()<<"当前时刻："<<CQtGpsSingle::GetSingelton()->getCurrentTime()<<"纬度："<<CQtGpsSingle::GetSingelton()->getLatitudeSign()
//                   <<CQtGpsSingle::GetSingelton()->getLatitude()<<"纬度"<<CQtGpsSingle::GetSingelton()->getLongitudeSign()<<CQtGpsSingle::GetSingelton()->getLongitude();
    });

#endif

#if 0 //测试GPS
    CQtGPS *gps=new CQtGPS(tr("ttyAMA3"),QSerialPort::Baud38400,this);
    gps->setParent(this);
//    gps->setPortName(tr("COM21"));
//    gps->setBaudRate(QSerialPort::Baud38400);
    gps->open(QIODevice::ReadWrite);
    if(gps->isOpen()==true)
    {
        qDebug()<<tr("open true");

        connect(gps,&CQtGPS::signalGetReady,this,[=]
        {
            this->ui->lineEditTime->setText(CQtGpsSingle::GetSingelton()->getCurrentTime().toString("yyyy-MM-dd hh:mm:ss"));
            qDebug()<<"当前时刻："<<gps->getCurrentTime()<<"纬度："<<gps->getLatitudeSign()<<gps->getLatitude()<<"纬度"<<gps->getLongitudeSign()<<gps->getLongitude();
        });
    }


#endif

#if 0 //测试直流电能表
    CDCElecEnerMeter *gps=new CDCElecEnerMeter(tr("COM21"),QSerialPort::Baud2400,this);
    gps->setParent(this);
    gps->open(QIODevice::ReadWrite);
    if(gps->isOpen()==true)
    {
        qDebug()<<tr("open true");
        gps->write("hello");
    }


#endif

#if 0 //测试交流电能表
    CACElecEnerMeter *gps=new CACElecEnerMeter(tr("COM21"),QSerialPort::Baud2400,this);
    gps->setParent(this);
    gps->open(QIODevice::ReadWrite);
    if(gps->isOpen()==true)
    {
        qDebug()<<tr("open true");
        gps->write("hello");
    }


#endif

#if 0 //测试交流电能表
    CDacControler *gps=new CDacControler(tr("COM21"),QSerialPort::Baud2400,this);
    gps->setParent(this);
    gps->open(QIODevice::ReadWrite);
    if(gps->isOpen()==true)
    {
        qDebug()<<tr("open true");
        //gps->write("hello");
        //gps->setDacOutput(C1,1.22,0);
        gps->set1RoadDacOutput(2.455);
        gps->setBatterySimulatorOutput(953.34);
    }


#endif
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_ptnSend_clicked()
{
#if 1 //测试饿汉单例模式
      CDacControlerSingle::GetSingelton()->set1RoadDacOutput(4.2);
      CQtGpsSingle::GetSingelton()->write("gps11");
    //if( CQtGpsSingle::GetSingelton()->isOpen()==true)
    {
//        qDebug()<<tr("open true");
//        CQtGpsSingle::GetSingelton()->write("hello");
           // CDacControlerSingle::GetSingelton()->write("hello");
        connect(CQtGpsSingle::GetSingelton(),&CQtGpsSingle::signalGetReady,this,[=]
        {
            CDacControlerSingle::GetSingelton()->write("get signal");
            this->ui->lineEditTime->setText(CQtGpsSingle::GetSingelton()->getCurrentTime().toString("yyyy-MM-dd hh:mm:ss"));
            //this->ui->lineEditTime->setText("QYROIUYEW");
//            qDebug()<<"当前时刻："<<CQtGpsSingle::GetSingelton()->getCurrentTime()<<"纬度："<<CQtGpsSingle::GetSingelton()->getLatitudeSign()
//                   <<CQtGpsSingle::GetSingelton()->getLatitude()<<"纬度"<<CQtGpsSingle::GetSingelton()->getLongitudeSign()<<CQtGpsSingle::GetSingelton()->getLongitude();
        });
    }

#endif
}
